/**
 * Copyright (c) 2005-2016, yinwer81 (yinwer81@aliyun.com)
 *
 * Licensed under the Apache License, Version 2.0
 */
package com.nozturn.throne.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import com.nozturn.throne.dao.AccessTokenDao;
import com.nozturn.throne.dao.AuthorizationDao;
import com.nozturn.throne.entity.AccessToken;
import com.nozturn.throne.entity.Authorization;
import com.nozturn.throne.entity.OAuthInfoDTO;
import com.nozturn.throne.service.ClientService;
import com.nozturn.throne.service.OAuthService;

@Service("OAuthServiceImpl")
public class OAuthServiceImpl implements OAuthService {

    // 以后接缓存了：
    @SuppressWarnings("unused")
    private Cache cache;

    @Autowired
    private AuthorizationDao authorizationDao;

    @Autowired
    private AccessTokenDao accessTokenDao;

    @Autowired
    private ClientService clientService;

    @Autowired
    public OAuthServiceImpl(final CacheManager cacheManager) {
        this.cache = cacheManager.getCache("code-cache");
    }
    
    public Authorization findByUserPkAndClientPk(final Long userPk, final Long clientPk){
        return authorizationDao.findByUserPkAndClientPk(userPk, clientPk);
    }

    public OAuthInfoDTO findAllByAuthorizationCode(final String authorizationCode) {
        return authorizationDao.findAllByAuthorizationCode(authorizationCode);
    }

    public OAuthInfoDTO findAllByAccessToken(final String accessToken) {
        return accessTokenDao.findAllByAccessToken(accessToken);
    }

    public int addAuthorizationCode(final String authorizationCode, final Long clientPk, final Long userPk) {
        // 以后接缓存了：
        // cache.put(authorizationCode, username);
        Authorization authorization = new Authorization();
        authorization.setAuthorizationCode(authorizationCode);
        authorization.setClientPk(clientPk);
        authorization.setCreatedBy("system");
        authorization.setUserPk(userPk);
        authorization.setValid(200);
        return authorizationDao.createAuthorization(authorization) != null ? 1 : 0;
    }

    public int addAccessToken(final String accessToken, final Long authorizationPk) {
        // 以后接缓存了：
        // cache.put(accessToken, username);
        AccessToken access = new AccessToken();
        access.setAccessToken(accessToken);
        access.setAuthorizationPk(authorizationPk);
        access.setExpiresIn(3600);
        access.setCreatedBy("system");
        access.setValid(200);
        return accessTokenDao.createAccessToken(access) != null ? 1 : 0;
    }

    public String getUsernameByAuthorizationCode(final String authorizationCode) {
        return findAllByAuthorizationCode(authorizationCode).getUsername();
        // 以后接缓存了：
        // return (String) cache.get(authorizationCode).get();
    }

    public String getUsernameByAccessToken(final String accessToken) {
        return findAllByAccessToken(accessToken).getUsername();
        // 以后接缓存了：
        // return (String) cache.get(accessToken).get();
    }

    public boolean checkAuthorizationCode(final String authorizationCode) {
        return authorizationDao.findByAuthorizationCode(authorizationCode) != null;
        // 以后接缓存了：
        // return cache.get(authorizationCode) != null;
    }

    public boolean checkAccessToken(final String accessToken) {
        return accessTokenDao.findByAccessToken(accessToken) != null;
        // 以后接缓存了：
        // return cache.get(accessToken) != null;
    }

    public boolean checkClientId(final String clientId) {
        return clientService.findByClientId(clientId) != null;
    }

    public boolean checkClientSecret(final String clientSecret) {
        return clientService.findByClientSecret(clientSecret) != null;
    }

    public int getExpireIn() {
        return 3600;
    }

}
